; Generated by Modix - Version 3.6.3 Macro A
; Use only after verifying connection between both nozzles and puck. 

var errorfix_primary = 0.000; constant offset

M291 S3 R"Setting the Z-offset Height" P"Press OK to continue, or CANCEL to abort"
if {global.idex} = 1
	M291 S2 R"Warning: Secondary Z-offset" P"The secondary Z-offset has been reset to stock values. You must re-calibrate the secondary Z-offset values after completing this calibration"

M300 S666 P666					 								; beep
G31 Z0															; set Z-offset to Z=0
echo >"0:/sys/config_probe.g" "G31 Z0"
if {global.idex} = 1
	G10 P1 Z0													; set secondary Z-offset to Z=0
	echo >"0:/sys/config_probe_secondary.g" "G10 P1 Z0"			; Set the Z-offset to 0 before starting

M291 T0 S4 R"Hotend Temp" P"Select the temperature you wish to run the Z-offset procedure at" K{"200","210","230","250","270"} 
	if input = 0
		M568 P0 S200 R200 A0
	if input = 1
		M568 P0 S210 R210 A2
	if input = 2
		M568 P0 S230 R230 A2
	if input = 3
		M568 P0 S250 R250 A2
	if input = 4
		M568 P0 S270 R270 A2

T0							; select tool 0 (primary hotend)
M116 P0 H5					; wait for temperature to reach within 5c of set temperature

M300 S666 P666 				; beep
M291 S3 R"Homing axes" P"Remove the puck. The printer will home the X, Y, and Z axises without the puck"

G28							; home all

T0							; reselect T0
G31 Z0						; set Z-offset to Z=0
M564 H1 S0					; Negative movements are allowed
G29 S2						; cancel mesh bed compensation
G90 						; absolute positioning
G1 Z20						; raise Z to 20mm
M83							; relative extruder moves
G1 E-5 F300 				; retract 5mm of filament
G4 P0						; wait for the moves to finish       
M291 S3 R"Clean the nozzle" P"The nozzle has been pre-heated. You must clean it from any plastic debris"
G1 X30 Y150 F6000			; go to X30, Y150
G4 P0						; wait for movements to have stopped


M300 S666 P666				; beep
M291 S3 P"Place the offset tool below the nozzle and press OK to perform the macro, or CANCEL to abort"

G4 P200						; wait 200ms
G91							; relative moves
G1 H1 Z-5 F90				; move 5mm down, stop when hitting the offset tool (but don't risk crashing the bed)
G92 Z0						; set current position as Z=0
G4 P0						; wait for moves to complete
G1 Z2 X3 F200				; move 2mm up and 3mm to the right
G1 H1 Z-4 F30				; move 5mm down, stop when hitting the offset tool (but don't risk crashing the bed)
G92 Z0						; set current position as Z=0
G4 P200						; wait 200ms
G1 Z6 F200					; move up 6mm

G91							; relative moves
G1 X{sensors.probes[0].offsets[0] * -1} Y{sensors.probes[0].offsets[1] * -1} F1500 ; move nozzle so that BLTouch is in it's position 
G90							; absolute moves

G30 S-3 					; probes Z and adjusts the probe trigger height to match the actual stop height.
G4 P200 					; wait 200ms
G91							; relative movement
M568 P0 R0 S0				; set tool 0 temperatures to 0

var Z_auto_offset = {sensors.probes[0].triggerHeight+var.errorfix_primary}	; math to calculate the BLTouch offset

	if var.Z_auto_offset < 0.01
		M291 S2 R"The nozzle has not touched the puck." P"Please check the guide for troubleshooting"
		echo "The nozzle has not touched the puck. Please check the guide for troubleshooting"
		echo >"0:/sys/config_probe.g" "G31 Z-3"
	elif var.Z_auto_offset >= 0.01 & var.Z_auto_offset < 3.5
		echo >"0:/sys/config_probe.g" "G31 Z"^{var.Z_auto_offset}
		M291 S2 R"Z-offset has been stored" P"the Z-offset has been stored in the config_probe.g file. Verify the value stored with the value displayed in the console" 
		echo "the Z-offset has been stored in the config_probe.g file. The Z-offset is", var.Z_auto_offset, "mm. Remove the offset tool and place it back in the box"
	elif var.Z_auto_offset >= 3.5
		M291 S2 R"Z-Offset is too high" P"Please verify the probe height using the probe-calibration"
		echo "Z-Offset is too high. Please verify the probe height using the probe-calibration"
		echo >"0:/sys/config_probe.g" "G31 Z-3"

M564 S1 H1      			; Negative movements are forbidden
M300 S666 P666 				; beep

G91							; relative moves
G1 Z5						; lift nozzle up 5mm
G90							; absolute moves

T-1							; deselect all tools
M18							; release steppers